package com.control.qct.businesssteward.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.CalendarViewAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 自定义假日历
 * Created by Administrator on 2017/12/29 0029.
 */

public class CustomCalendarView extends LinearLayout {
    private ImageView mIvDateReduce;
    private TextView mTvDate;
    private ImageView mIvDateAdd;
    private RecyclerView mRvCalendar;
    private Calendar mCalendar;
    private int mMaxDay;
    private Date mDate;
    private List<Integer> mDayList = new ArrayList<>();
    private CalendarViewAdapter mCalendarViewAdapter;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    private String mNowTime;
    private String[] mTime;
    private String mNowDay;
    private String mDateTime;

    public CustomCalendarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.calendar_date_tool, this);
        findViewById();
        init(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        mCalendar = Calendar.getInstance();// 得到一个Calendar的实例
        mDate = new Date();
        mCalendar.setTime(mDate);//设置当前时间
        mMaxDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获取当月最大天数
        mDateTime = sdf.format(mDate);
        mNowTime = sdf1.format(mDate);
        mTime = mNowTime.split("-");
        mNowDay = mTime[2];
        mTvDate.setText(mDateTime);
        getDayNum();
        mRvCalendar.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        mCalendarViewAdapter = new CalendarViewAdapter(mDayList);
        mRvCalendar.setAdapter(mCalendarViewAdapter);
        mRvCalendar.smoothScrollToPosition(Integer.parseInt(mNowDay) + 2);//将日期移到当前时间
        mCalendarViewAdapter.setPosition(Integer.parseInt(mNowDay) - 1);//默认选择当前时间
        setOnClickListener();
    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        mIvDateReduce.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setDateTime(-1);
                mICallbackClickListener.leftClick(mDateTime);
            }
        });
        mIvDateAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setDateTime(+1);
                mICallbackClickListener.rightClick(mDateTime);
            }
        });

        mCalendarViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mCalendarViewAdapter.setPosition(position);
                int day = position + 1;
                mNowTime = mDateTime + "-" + (day < 10 ? "0" + day : day);
                mTime = mNowTime.split("-");
                mNowDay = mTime[2];
                mICallbackClickListener.itemClick(mNowTime);
            }
        });
    }

    public void setVisibility(boolean show) {
        mRvCalendar.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 日期选择
     *
     * @param i
     */
    private void setDateTime(int i) {
        mCalendar.add(Calendar.MONTH, i);
        mDate = mCalendar.getTime();
        mCalendar.setTime(mDate);
        mDateTime = sdf.format(mDate);
        mTvDate.setText(mDateTime);
        mMaxDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        getDayNum();
        //将之前选择的日期时间滑动到中间位置
        mRvCalendar.smoothScrollToPosition(mNowTime.contains(sdf.format(mDate)) ? Integer.parseInt(mNowDay) + 2 : 0);
        //标记之前选择的日期时间
        mCalendarViewAdapter.setPosition(mNowTime.contains(sdf.format(mDate)) ? Integer.parseInt(mNowDay) - 1 : -1);
    }

    /**
     * 实例化控件
     */
    private void findViewById() {
        mIvDateReduce = findViewById(R.id.iv_date_reduce);
        mIvDateAdd = findViewById(R.id.iv_date_add);
        mTvDate = findViewById(R.id.tv_date);
        mRvCalendar = findViewById(R.id.rv_calendar);
    }

    public void setRvCalendarGone(boolean show) {
        mRvCalendar.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 获取每月的天数
     */
    private void getDayNum() {
        mDayList.clear();
        for (int i = 1; i <= mMaxDay; i++) {
            mDayList.add(i);
        }
        if (mCalendarViewAdapter != null) {
            mCalendarViewAdapter.notifyDataSetChanged();
            mRvCalendar.smoothScrollToPosition(0);
        }
    }

    private ICallbackClickListener mICallbackClickListener;

    /**
     * 设置监听对象
     *
     * @param listener
     */
    public void setICallbackClickListener(ICallbackClickListener listener) {
        this.mICallbackClickListener = listener;
    }

    /**
     * 点击事件回调监听
     */
    public interface ICallbackClickListener {
        void leftClick(String time);//月份减少

        void rightClick(String time);//月份增加

        void itemClick(String time);//选择日期
    }

}
